﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Register.Model;
using Register.BLL;
using System.Web.Script.Serialization;
namespace RegisterWeb.Manager.SystemUserManager.ashx
{
    /// <summary>
    /// RoleService 的摘要说明
    /// </summary>
    public class RoleService : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
          string action=  context.Request["action"];
          action = action;
          if (action == "get")
          {
              int intPageSize = int.Parse(context.Request["rows"].ToString());
              int intCurrentPage = int.Parse(context.Request["page"].ToString());
              var list=    RoleBLL.GetAllRole("1=1", intCurrentPage, intPageSize);
              RoleListJSON json = new RoleListJSON();
              json.rows = list;
              json.total = RoleBLL.GetRoleCount(null);
              JavaScriptSerializer jss = new JavaScriptSerializer();
              context.Response.Write(jss.Serialize(json));
          }
          else if (action == "add")
          {
          Role role=new Role();

          role.Role_ID=  context.Request["Role_ID"]; 
          role.Role_Name= context.Request["Role_Name"];
          role.Role_Describe= context.Request["Role_Describe"];
          role.Role_Degree= context.Request["Role_Degree"];
          role.Role_AddTime=DateTime.Now;
          role.Role_Recoder = "1";//以后用SESSTION
          role.Role_LastAmendPerson = "1";
          role.Role_LastAmendTime= DateTime.Now;
          RoleBLL.AddRole(role);
          JavaScriptSerializer jss = new JavaScriptSerializer();
          context.Response.Clear();
             string j= jss.Serialize(new message(true,"角色添加成功"));
          context.Response.Write(j);

          context.Response.End();
          }
          else if (action == "delete")
          {
              String id = context.Request["id"]; 
              RoleBLL.DeleteRole(id);
             context.Response.Write("success");
          }
          else if (action == "updata")
          {
              Role role = new Role();

              role.Role_ID = context.Request["id"];
              role.Role_Name = context.Request["Role_Name"];
              role.Role_Describe = context.Request["Role_Describe"];
              role.Role_Degree = context.Request["Role_Degree"];
        //      role.Role_AddTime = DateTime.Now;
          //    role.Role_Recoder = "1";//以后用SESSTION
              role.Role_LastAmendPerson = "1";
              role.Role_LastAmendTime = DateTime.Now;
              RoleBLL.UpdateRole(role);
                      JavaScriptSerializer jss = new JavaScriptSerializer();
              string j = jss.Serialize(new message(true, "角色修改成功"));
              context.Response.Write(j);
          }
        }

  


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
    class RoleListJSON
    {
        public int total { get; set; }
        public List<Role> rows { get; set; }
    }
    //class message
    //{
    //    public message(bool success,string msg)
    //    {
    //        this.msg = msg;
    //        this.success = success;
    //    }
    //    public bool success { get; set; }
    //    public String msg { get; set; }
    //}

}